package com.ruoyi.project.kbjz.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.kbjz.domain.KbjzPatient;
import com.ruoyi.project.kbjz.domain.KbjzRegister;
import com.ruoyi.project.kbjz.service.IKbjzPatientService;
import com.ruoyi.project.kbjz.service.IKbjzRegisterService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysRegFee;
import com.ruoyi.project.system.service.ISysDeptService;
import com.ruoyi.project.system.service.ISysRegFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/kbjz/patient")
public class KbjzPatientController extends BaseController {
    @Autowired
    private IKbjzPatientService patientService;
    @Autowired
    private ISysRegFeeService sysRegFeeService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IKbjzRegisterService registerService;

    /**
     * 查询患者是否已经录入到该系统
     * @param patientCard 患者身份证信息
     * @return
     */
    @GetMapping("/selectByCard/{patientCard}")
    public AjaxResult selectPatient(@PathVariable String patientCard){
        //从缓存中查询
        KbjzPatient patient = (KbjzPatient) redisTemplate.opsForValue().get(patientCard);
        if (patient == null){
            //从数据库中查询
            patient = patientService.selectPatientByPatientCard(patientCard);
            if (patient == null){
                return AjaxResult.error(200,"患者还未录入系统");
            }
            //将查询结果加入到缓存中
            redisTemplate.opsForValue().set(patientCard,patient);
        }
       return AjaxResult.success(patient);
    }
    @GetMapping("/selectRegFeeInfo")
    public AjaxResult selectRegFeeInfo(SysRegFee sysRegFee){
        List<SysRegFee> sysRegFees = sysRegFeeService.selectAllRegFee(sysRegFee);
        return AjaxResult.success(sysRegFees);
    }
    @GetMapping("/getDeptInfo")
    public TableDataInfo getDeptInfo(SysDept sysDept){
        startPage();
        List<SysDept> sysDepts = sysDeptService.selectDeptList(sysDept);
        return  getDataTable(sysDepts);
    }
    @PostMapping("/insertPatientAndRegister")
    public AjaxResult insertPatientAndRegister(KbjzPatient patient,KbjzRegister kbjzRegister){
        int pIndex = patientService.addPatient(patient);
        kbjzRegister.setPatientId(patient.getPatientId());
        int rIndex = registerService.addRegister(kbjzRegister);
        if (pIndex ==1 && rIndex == 1){
            return AjaxResult.success("挂号成功");
        }
        return AjaxResult.error("支付失败");
    }
    @PostMapping("/selectDeptInfo")
    public TableDataInfo selectDeptInfo(KbjzRegister kbjzRegister){
        //KbjzRegister( deptId=102,  registerDate=Mon Sep 13 00:00:00 CST 2021, rfeeId=2, registerPotime=1)
        //List<Long> deptids = registerService.getDeptIdByRegisterCondition(kbjzRegister);
        List<SysDept> deptList = new ArrayList<>();
        List<Long>  deptids = registerService.getDeptIdByRegisterCondition(kbjzRegister);

        startPage();
        for (Long deptid : deptids) {
            SysDept sysDept = sysDeptService.selectDeptById(deptid);
            deptList.add(sysDept);
        }
        return getDataTable(deptList);
    }
}
